package org.ala.tcp_feign_common.utils;

import java.lang.reflect.Method;

import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 通过method拿url注解
 *
 * @author 骆毅(ala)
 * @date 2024年4月22日
 */
public class MethodUrlUtils {

	
	/**
	 * 取请求的url
	 * <p>	先找本类，找不到找父类，还找不到找实现的接口
	 * PostMapping | GetMapping | PutMapping | PatchMapping | RequestMapping都有可能
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 */
	public static String findUrl(Method method) {
		//	直接从method中查找注解
		String url = url(method);
		if (StringUtils.hasText(url)) {return url;}
		
		//	如果找不到从父类中找继承的method里找
		Class<?> superClass = method.getDeclaringClass().getSuperclass();
		if (superClass != null) {
			Method superMethod = null;
			try {superMethod = ClassUtils.getMethod(superClass, method.getName(), method.getParameterTypes());}
			catch (Exception e) {}
			if (superMethod != null) {
				url = findUrl(superMethod);
				if (StringUtils.hasText(url)) {return url;}
			}
		}
		
		//	如果找不到从继承的接口里找
		Class<?>[] interfaces = method.getDeclaringClass().getInterfaces();
		if (interfaces != null && interfaces.length > 0) {
			for (Class<?> intf : interfaces) {
				Method intfMethod = null;
				try {intfMethod = ClassUtils.getMethod(intf, method.getName(), method.getParameterTypes());}
				catch (Exception e) {}
				if (intfMethod != null) {
					url = findUrl(intfMethod);
					if (StringUtils.hasText(url)) {return url;}
				}
			}
		}
		return url;
	}
	/**
	 * 取请求的url
	 * PostMapping | GetMapping | PutMapping | PatchMapping | RequestMapping都有可能
	 */
	public static String url(Method method) {
		String url = null;
		if (!StringUtils.hasText(url)) {
			url = urlFromPostMapping(method.getDeclaredAnnotation(PostMapping.class));
			if (!StringUtils.hasText(url)) url = urlFromGetMapping(method.getDeclaredAnnotation(GetMapping.class));
			if (!StringUtils.hasText(url)) url = urlFromPutMapping(method.getDeclaredAnnotation(PutMapping.class));
			if (!StringUtils.hasText(url)) url = urlFromPatchMapping(method.getDeclaredAnnotation(PatchMapping.class));
			if (!StringUtils.hasText(url)) url = urlFromRequestMapping(method.getDeclaredAnnotation(RequestMapping.class));
		}
		return url;
	}
	/**
	 * 
	 */
	protected static String urlFromPostMapping(PostMapping mapping) {
		String url = null;
		if (mapping != null) {
			String name = mapping.name();
			if (!StringUtils.hasText(url)) url = name;
			String[] values = mapping.value();
			if (!StringUtils.hasText(url) && values.length > 0) url = values[0];
			String[] paths = mapping.path();
			if (!StringUtils.hasText(url) && paths.length > 0) url = paths[0];
		}
		return url;
	}
	protected static String urlFromGetMapping(GetMapping mapping) {
		String url = null;
		if (mapping != null) {
			String name = mapping.name();
			if (!StringUtils.hasText(url)) url = name;
			String[] values = mapping.value();
			if (!StringUtils.hasText(url) && values.length > 0) url = values[0];
			String[] paths = mapping.path();
			if (!StringUtils.hasText(url) && paths.length > 0) url = paths[0];
		}
		return url;
	}
	protected static String urlFromPutMapping(PutMapping mapping) {
		String url = null;
		if (mapping != null) {
			String name = mapping.name();
			if (!StringUtils.hasText(url)) url = name;
			String[] values = mapping.value();
			if (!StringUtils.hasText(url) && values.length > 0) url = values[0];
			String[] paths = mapping.path();
			if (!StringUtils.hasText(url) && paths.length > 0) url = paths[0];
		}
		return url;
	}
	protected static String urlFromPatchMapping(PatchMapping mapping) {
		String url = null;
		if (mapping != null) {
			String name = mapping.name();
			if (!StringUtils.hasText(url)) url = name;
			String[] values = mapping.value();
			if (!StringUtils.hasText(url) && values.length > 0) url = values[0];
			String[] paths = mapping.path();
			if (!StringUtils.hasText(url) && paths.length > 0) url = paths[0];
		}
		return url;
	}
	protected static String urlFromRequestMapping(RequestMapping mapping) {
		String url = null;
		if (mapping != null) {
			String name = mapping.name();
			if (!StringUtils.hasText(url)) url = name;
			String[] values = mapping.value();
			if (!StringUtils.hasText(url) && values.length > 0) url = values[0];
			String[] paths = mapping.path();
			if (!StringUtils.hasText(url) && paths.length > 0) url = paths[0];
		}
		return url;
	}
	
}
